/**
 * @file		interrupt.c
 * @brief		interrupt control function
 * @note		None
 * @attention	None
 * 
 * <B><I>ALL RIGHTS RESERVED, COPYRIGHT&copy; SOCIONEXT INCORPORATED 2016</I></B>
 */

/* Driver includes. */
#include "dd_ipcu.h"
#include "dd_pmc_adc.h"
#include "dd_pmc_ctc.h"
#include "dd_pmc_hdmac.h"
#include "dd_pmc_i2c.h"
#include "dd_pmc_spi.h"
#include "dd_pmc_temp_sensor_if.h"
#include "dd_pmc_tmr32.h"
#include "dd_pmc_uart.h"
#include "dd_pmc_watchdog.h"
#include "dd_pmc_exiu.h"
#include "dd_pmc_crg.h"
#include "dd_arm.h"

#include "statemachine.h"

#include "user_custom.h"

extern unsigned custom_EXIU_Int_Handler( void );

void vInterruptHdmacCh0(void)
{
	// HDMAC Interrupt ch0.
	Dd_PMC_HDMAC_Int_Handler(0);
}

void vInterruptHdmacCh1(void)
{
	// HDMAC Interrupt ch1.
	Dd_PMC_HDMAC_Int_Handler(1);
}

void vInterruptHdmacCh2(void)
{
	// HDMAC Interrupt ch2.
	Dd_PMC_HDMAC_Int_Handler(2);
}

void vInterruptHdmacCh3(void)
{
	// HDMAC Interrupt ch3.
	Dd_PMC_HDMAC_Int_Handler(3);
}

void vInterruptHdmacCh4(void)
{
	// HDMAC Interrupt ch4.
	Dd_PMC_HDMAC_Int_Handler(4);
}

void vInterruptUartTx(void)
{
	// UART Interrupt Tx.
	Dd_PMC_UART_Int_Handler_Tx();
}

void vInterruptUartRx(void)
{
	// UART Interrupt Rx.
	Dd_PMC_UART_Int_Handler_Rx();
}

void vInterruptSpi(void)
{
	// SPI Interrupt.
	Dd_PMC_SPI_Int_Handler();
}

void vInterruptCalendar(void)
{
	// Calendar Interrupt.
}

void vInterruptTimer32(void)
{
	// 32bit Reload Timer Interrupt.
	Dd_PMC_TMR32_Int_Handler();
}

void vInterruptAdc(void)
{
	// ADC Interrupt.
	Dd_PMC_ADC_Int_Handler();
}

void vInterruptI2c(void)
{
	// I2C Interrupt.
	Dd_PMC_I2C_Int_Handler();
}

void vInterruptWatchdog(void)
{
	// Watchdog Timer Interrupt.
	Dd_PMC_Wdog_Int_Handler();
}

void vInterruptTempSensorIf(void)
{
	// Temperature Sensor IF Interrupt.
	Dd_PMC_TS_IF_Int_Handler();
}

void vInterruptCtc(void)
{
	// CTC Interrupt.
	Dd_PMC_CTC_Int_Handler();
}

void vInterruptIpcuCh0(void)
{
	// IPCU Interrupt ch0.
	Dd_IPCU_Int_Handler(12);
}

void vInterruptIpcuCh1(void)
{
	// IPCU Interrupt ch1.
	Dd_IPCU_Int_Handler(13);
}

void vInterruptIpcuCh2(void)
{
	// IPCU Interrupt ch2.
	Dd_IPCU_Int_Handler(14);
}

void vInterruptIpcuCh3(void)
{
	// IPCU Interrupt ch3.
	Dd_IPCU_Int_Handler(15);
}

void vInterruptGpio(void)
{
	unsigned int			val = 0;
	E_STATEMACHINE_STATE	status;
	
	if(custom_EXIU_Int_Handler() == FALSE){
		Dd_PMC_EXIU_Get_EIREQSTA(15, &val);
		if (val == 1) {
			Dd_PMC_EXIU_Set_EIREQCLR_Ch(15);
			
			status = iStateMachineGetState();
			if (status == STATE_STANDBY) {
				// Wake-up
				iStateMachineSetStateFromISR( STATE_WAKEUP );
			}
		}
		#ifdef BOARD_TYPE_SNI_SMALL
		Dd_PMC_EXIU_Get_EIREQSTA(0, &val);
		if (val == 1) {
			Dd_PMC_EXIU_Set_EIREQCLR_Ch(0);
			
			status = iStateMachineGetState();
			if (status == STATE_NORMAL) {
				// Terminate
				iStateMachineSetStateFromISR( STATE_TERM );
			}
		}
		#endif	// BOARD_TYPE_SNI_SMALL
	}
}

